﻿using Ec.NETCoreFramework.Common.Core;
using Ec.NETCoreFramework.Common.Core.Helpers;
using Ec.NETCoreFramework.Common.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Threading;
using System.Diagnostics;
using Ec.NETCoreFramework.Common.Plug.Core;
using System.Linq;

namespace MQ_Approval
{
    /// <summary>
    /// 汇报
    /// </summary>
    public class ManualSyncApproval : ApprovalSync
    {
        private string logTitle = "ManualSyncApproval";
        private string _SoftwareVersion = string.Empty;

        public ManualSyncApproval(RequestAppSecret appSecret, ConsumeConfig consumeConfig) :base(appSecret,consumeConfig)
        {
            _SoftwareVersion = AppsettingsHelper.app(new string[] { "SoftwareVersion" });

        }
        public override async Task Excute(Dictionary<string, object> dic)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            //拉数据
            try
            {
                var item = new ApprovalOutput();
                item.SetValues(dic);
                //手动同步
                await ManualSync(item);

                stopwatch.Stop();
                Console.WriteLine($"【{_consumeConfig.Domain}-{logTitle}-{_SoftwareVersion}】此次执行结束，耗时{stopwatch.ElapsedMilliseconds}ms");
            }
            catch (Exception ex)
            {

                if (stopwatch.IsRunning)
                {
                    stopwatch.Stop();
                }
                Console.WriteLine($"【{logTitle}】:{ex.Message}");
            }
        }
        /// <summary>
        /// 获取汇报列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private async Task ManualSync(ApprovalOutput item)
        {
            var dt_month_start = item.start_time == 0 ? DateTime.Now : Utils.UnixTimeToTime(item.start_time.ToString(), 10);
            var dt_month_ends= item.end_time == 0 ? DateTime.Now : Utils.UnixTimeToTime(item.end_time.ToString(), 10);
            var end = 0L;

            while (DateTime.Now > dt_month_start)
            {
                var begin = Utils.ToUnixTime(dt_month_start, 10);

                //当前月底
                var dt_month_end = dt_month_start.AddDays(15);

                //如果当前月最后一天大于当前时间
                if (dt_month_end > dt_month_ends)
                {

                    end = Utils.ToUnixTime(dt_month_ends, 10);
                }
                else
                {
                    end = Utils.ToUnixTime(dt_month_end, 10);
                }
                //获取汇报列表
                await GetApprovalList(item, begin, end, "0");
                //获取汇报详情
                await GetApprovalDetail(item);

                dt_month_start = dt_month_end;
            }
        }
    }

}
